package com.ns.scs.msg.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.basic.server.mapper.ServerEntryMapper;
import com.ns.core.current.config.SymbolsMark;
import com.ns.scs.msg.dto.MsgPageDto;
import com.ns.scs.msg.entry.MsgEntry;
import com.ns.scs.msg.entry.MsgUser;
import com.ns.scs.msg.mapper.MsgMapper;
import com.ns.scs.msg.mapper.MsgUserMapper;
import com.ns.scs.msg.vo.MsgVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 参数业务实现
 *
 * @author lw
 * @since 2024/09/23
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MsgServiceImpl extends ServiceImpl<MsgMapper, MsgEntry> implements IMsgService {

    private final MsgMapper mapper;

    private final MsgUserMapper msgUserMapper;

    private final ServerEntryMapper serverEntryMapper;

    @Override
    public Page<MsgVO> getList(MsgPageDto dto) {
        return mapper.getList(dto, new Page<>(dto.getCurrent(), dto.getSize()));
    }

    @Override
    public String sendInnerMsg(Long id, Long sid, String msgTemplate, List<String> params) {
        MsgEntry entry = new MsgEntry();
        entry.setType(SymbolsMark.ZERO_S);
        String msg = String.format(msgTemplate, params.toArray());
        entry.setMsgText(msg);
        entry.setStatus(1);
        entry.setSid(sid);
        Long sort = mapper.selectCount(null);
        entry.setSort(ObjectUtils.isEmpty(sort) ? 1 : sort + 1);
        mapper.insert(entry);
        MsgUser msgUser = new MsgUser();
        msgUser.setMsgId(entry.getId());
        msgUser.setUserId(id);
        msgUser.setStatus(1);
        msgUserMapper.insert(msgUser);
        return msg;
    }
}
